package com.cnpc.framework.base.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cnpc.framework.base.entity.Org;
import com.cnpc.framework.base.entity.User;
import com.cnpc.framework.base.service.BaseService;
import com.cnpc.framework.utils.SecurityUtil;

@Controller
public class MainController {

    Logger logger = Logger.getLogger(MainController.class);
    private static final String COMPANY = "COMPANY";
    private static final String ICON_URL = "ICON_URL";
    
    @Resource
    private BaseService baseService;

    @RequestMapping(method = RequestMethod.GET, value = "/")
    public String index(HttpServletRequest request) {
        //首页加载邮件消息 message/count  user/getAvatar
    	User user = SecurityUtil.getUser();
    	Org org = baseService.get(Org.class, user.getDeptId());
    	if(null != org) {
    		request.setAttribute(COMPANY, org.getName());
    	}else {
    		request.setAttribute(COMPANY, "天府大弘");
    	}
    	if(null == org || StringUtils.isEmpty(org.getIcon())) {
    		request.setAttribute(ICON_URL, "/resources/common/images/dh-logo.png");
    	}else{
    		request.setAttribute(ICON_URL, org.getIcon());
    	}
        return "main";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/main")
    public String main(HttpServletRequest request) {
    	User user = SecurityUtil.getUser();
    	Org org = baseService.get(Org.class, user.getDeptId());
    	if(null != org) {
    		request.setAttribute(COMPANY, org.getName());
    	}else {
    		request.setAttribute(COMPANY, "天府大弘");
    	}
    	if(null == org || StringUtils.isEmpty(org.getIcon())) {
    		request.setAttribute(ICON_URL, "/resources/common/images/dh-logo.png");
    	}else{
    		request.setAttribute(ICON_URL, org.getIcon());
    	}
        return "main";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/homePage")
    public String homePage(HttpServletRequest request) {

        return "homePage";
    }


}
